package com.algorithms.linkedlists;

import com.algorithms.utils.AlgorithmException;

/**
 * this stack is implemented by linked-list. <br>
 * the advantage is no need to define max number of items (therefore
 * memory is only used when necessary) 
 * 
 * @author minhld
 *
 * @param <T>
 */
public class LinkedStack<T> {
	private LinkedList<T> linkedData;

	public LinkedStack() {
		this.linkedData = new LinkedList<>();
	}
	
	public T pop() throws AlgorithmException {
		if (isEmpty()){
			throw new AlgorithmException("linked stack is empty.");
		}else{
			return this.linkedData.deleteFirst().getContent();
		}
	}

	/**
	 * no limit adding 
	 * 
	 * @param value
	 */
	public void push(T value) {
		this.linkedData.insertFirst(value);
	}

	public T peak() {
		return this.linkedData.getFirst().getContent();
	}

	public boolean isEmpty() {
		return linkedData.isEmpty();
	}
		
}
